const { secretOrKey } = require('../config/keys');
const User = require('../models/UserModel');

const JwtStrategy = require('passport-jwt').Strategy,
    ExtractJwt = require('passport-jwt').ExtractJwt;
const opts = {}
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = secretOrKey;

module.exports = passport => {
    passport.use(
        new JwtStrategy(opts, async (jwt_payload, done) => {
            // console.log(jwt_payload);
            const user = await User.findById(jwt_payload.id, {password: 0, __v: 0, date: 0});
            if (user) {
                return done(null, user);
            }
            return done(null, false);
        })
    );

};
